/**
 * Created by wenchuan.li on 2018/6/14 11:24.
 */
define(function () {

    var self,
        app = win.app,
        userInfo,
        customersArg,
        cdata = {
            GWAL001: '',
            GWAL003: '',
            GWAK001: '',
            GWAKL004: '',
            GWAL010: '',
            GWAL011: '',
            GWAL012: '',
            GWAL013: '',
            GWAL014: '',
            GWAL015: '',
            GWAL016: '',
            GWAL017: '',
            GWAL018: '',
            GWAL019: '',
            GWAL020: '',
            GWAL021: '',
            type: ''
        },
        areas = [],
        provinces = [],
        citys = [],
        districts = [],
        villages = [],
        streets = []
    ;

    var CustomersAdd = app.Module.extend({

        init: function () {
            self = this;
            self._super({
                _scroll: '.mui-scroll-wrapper',
                _readyBefore: function () {
                    customersArg = app.pageParameters['khlxrinfo_page'];
                    cdata = app.cloneJSON(customersArg.item);
                    cdata.type = customersArg.type;
                    if(cdata.type == 'add'){
                        cdata.GWAL001 = cdata.GWAK001;
                    }
                }
            }, win);
            userInfo = app.pageModule('p_main').userInfo;
            cdata.GWAK017 = userInfo.id;
            cdata.GWAK018 = userInfo.mobile;
        },
        checkPhone: function () {

            app.ajax({
                xurl: "api/wx/gwat100/checkCustomerPhone",
                loading: true,
                xdata: {
                    GWAK030: cdata.GWAK030
                },
                cb: function (result) {
                    if (result.ok) {
                        var count = result.data.count;
                        if (count > 0) {
                            app.alert({
                                msg: '客户手机号已经存在'
                            });
                        } else {
                            self.subData();
                        }
                    }
                }
            });
        },
        subData: function () {
            app.ajax({
                xurl: "api/wx/gwat900/saveGwal",
                loading: true,
                xdata: cdata,
                cb: function (result) {
                    if (result.ok) {
                        app.alert({
                            msg: '客户联系人添加成功！',
                            ok: function () {
                                app.back(1);
                            }
                        });
                        customersArg.cb();
                    }
                }
            });
        },
        getArea: function () {
            app.ajax({
                xurl: "api/wx/gwat900/getArea",
                xdata: {GWAD001: '3', GWAD003: 'CN'},
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        if (cdata.type == 'add') {
                            cdata.GWAL012 = datas[0].GWAD002;
                        }
                        areas.pushArray(datas, true);
                        self.getProvince();
                    }
                }
            });
        },
        getProvince: function () {
            app.ajax({
                xurl: "api/wx/gwat900/getArea",
                xdata: {GWAD001: '4', GWAD003: cdata.GWAL012},
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        if (datas.length > 0) {
                            if (cdata.type == 'add') {
                                cdata.GWAL013 = datas[0].GWAD002;
                            }
                            provinces.pushArray(datas, true);
                            self.getCity();
                        } else {
                            cdata.GWAL013 = '';
                            cdata.GWAL014 = '';
                            cdata.GWAL015 = '';
                            cdata.GWAL016 = '';
                            cdata.GWAL017 = '';
                            provinces.removeAll();
                            citys.removeAll();
                            districts.removeAll();
                            villages.removeAll();
                            streets.removeAll();
                        }
                    }
                }
            });
        },
        getCity: function () {
            app.ajax({
                xurl: "api/wx/gwat900/getArea",
                xdata: {GWAD001: '5', GWAD003: cdata.GWAL013},
                before: function (b) {
                    return Boolean(cdata.GWAL013);
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        if (datas.length > 0) {
                            if (cdata.type == 'add') {
                                cdata.GWAL014 = datas[0].GWAD002;
                            }
                            citys.pushArray(datas, true);
                            self.getDistricts();
                        } else {
                            cdata.GWAL014 = '';
                            cdata.GWAL015 = '';
                            cdata.GWAL016 = '';
                            cdata.GWAL017 = '';
                            citys.removeAll();
                            districts.removeAll();
                            villages.removeAll();
                            streets.removeAll();
                        }
                    }
                }
            });
        },
        getDistricts: function () {
            app.ajax({
                xurl: "api/wx/gwat900/getArea",
                xdata: {GWAD001: '6', GWAD003: cdata.GWAL014},
                before: function (b) {
                    return Boolean(cdata.GWAL014);
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        if (datas.length > 0) {
                            if (cdata.type == 'add') {
                                cdata.GWAL015 = datas[0].GWAD002;
                            }
                            districts.pushArray(datas, true);
                            self.getVillages();
                        } else {
                            cdata.GWAL015 = '';
                            cdata.GWAL016 = '';
                            cdata.GWAL017 = '';
                            districts.removeAll();
                            villages.removeAll();
                            streets.removeAll();
                        }
                    }
                }
            });
        },
        getVillages: function () {
            app.ajax({
                xurl: "api/wx/gwat900/getArea",
                xdata: {GWAD001: '7', GWAD003: cdata.GWAL015},
                before: function (b) {
                    return Boolean(cdata.GWAL015);
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        if (datas.length > 0) {
                            if (cdata.type == 'add') {
                                cdata.GWAL016 = datas[0].GWAD002;
                            }
                            villages.pushArray(datas, true);
                            self.getStreets();
                        } else {
                            cdata.GWAL016 = '';
                            cdata.GWAL017 = '';
                            villages.removeAll();
                            streets.removeAll();
                        }
                    }
                }
            });
        },
        getStreets: function () {
            app.ajax({
                xurl: "api/wx/gwat900/getArea",
                xdata: {GWAD001: '8', GWAD003: cdata.GWAL016},
                before: function (b) {
                    return Boolean(cdata.GWAL016);
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        if (datas.length > 0) {
                            if (cdata.type == 'add') {
                                cdata.GWAL017 = datas[0].GWAD002;
                            }
                            streets.pushArray(datas, true);
                        } else {
                            streets.removeAll();
                        }
                    }
                }
            });
        },

        _lazy: function () {
            this._super();
            self.getArea();
            app.ajax({
                xurl: "api/wx/gwat900/getArea",
                xdata: {GWAD001: '1', GWAD003: ''},
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        cdata.GWAL010 = datas[0].GWAD002;
                        app.ajax({
                            xurl: "api/wx/gwat900/getArea",
                            xdata: {GWAD001: '2', GWAD003: cdata.GWAL010},
                            cb: function (result) {
                                if (result.ok) {
                                    var datas = result.data.data;
                                    cdata.GWAL011 = datas[0].GWAD002;
                                }
                            }
                        });
                    }
                }
            });

        },
        _ready: function () {
            new Vue({
                el: doc.querySelector('.mui-content'),
                data: {
                    sub: cdata,
                    areas: areas,
                    provinces: provinces,
                    citys: citys,
                    districts: districts,
                    villages: villages,
                    streets: streets
                },
                methods: {
                    areaChange: function () {
                        cdata.GWAL013 = '';
                        cdata.GWAL014 = '';
                        cdata.GWAL015 = '';
                        cdata.GWAL016 = '';
                        cdata.GWAL017 = '';
                        provinces.removeAll();
                        citys.removeAll();
                        districts.removeAll();
                        villages.removeAll();
                        streets.removeAll();
                        self.getProvince();
                    },
                    provinceChange: function () {
                        cdata.GWAL014 = '';
                        cdata.GWAL015 = '';
                        cdata.GWAL016 = '';
                        cdata.GWAL017 = '';
                        citys.removeAll();
                        districts.removeAll();
                        villages.removeAll();
                        streets.removeAll();
                        self.getCity();
                    },
                    cityChange: function () {
                        cdata.GWAL015 = '';
                        cdata.GWAL016 = '';
                        cdata.GWAL017 = '';
                        districts.removeAll();
                        villages.removeAll();
                        streets.removeAll();
                        self.getDistricts();
                    },
                    districtChange: function () {
                        cdata.GWAL016 = '';
                        cdata.GWAL017 = '';
                        villages.removeAll();
                        streets.removeAll();
                        self.getVillages();
                    },
                    villageChange: function () {
                        cdata.GWAL017 = '';
                        streets.removeAll();
                        self.getStreets();
                    },
                    saveData: function () {
                        if (!cdata.GWAL003 || cdata.GWAL003 == '') {
                            app.toast('地址类型不能为空');
                            return;
                        }
                        if (!cdata.GWAL021 || cdata.GWAL021 == '') {
                            app.toast('联系人不能为空');
                            return;
                        }
                        if (!cdata.GWAL020 || cdata.GWAL020 == '') {
                            app.toast('电话不能为空');
                            return;
                        }
                        if (!cdata.GWAL019 || cdata.GWAL019 == '') {
                            app.toast('邮编不能为空');
                            return;
                        }
                        if (!cdata.GWAL012 || cdata.GWAL012 == '') {
                            app.toast('区域不能为空');
                            return;
                        }
                        if (!cdata.GWAL013 || cdata.GWAL013 == '') {
                            app.toast('省/市不能为空');
                            return;
                        }
                        if (!cdata.GWAL014 || cdata.GWAL014 == '') {
                            app.toast('市/区不能为空');
                            return;
                        }
                        if (!cdata.GWAL015 || cdata.GWAL015 == '') {
                            app.toast('县/区不能为空');
                            return;
                        }
                        if (!cdata.GWAL018 || cdata.GWAL018 == '') {
                            app.toast('详细地址不能为空');
                            return;
                        }

                        if (cdata.GWAL003=='Y' && customersArg.addr.oftenCount > 0){
                            if (cdata.type == 'add') {
                                app.toast('常用地址已存在！');
                                return;
                            }else if (cdata.GWAL002 != customersArg.addr.seq){
                                app.toast('常用地址已存在！');
                                return;
                            }
                        }

                        app.confirm({
                            msg: '是否保存客户联系人信息？',
                            ok: function () {
                                self.subData();
                            }
                        });
                    }
                }
            });
        }
    });

    new CustomersAdd();

});